<?php

namespace app\api\controller\v2;

use app\api\controller\Common;

class Coupons extends Common
{
	var $modelDb;
	public function _initialize()
	{
		parent::_initialize();
		$this->modelDb = model("Coupons");
	}
	//我的权益列表
	public function coupons_list()
	{
		$state = isset($this->postData['state']) ? $this->postData['state'] : 'all';
		$page = isset($this->postData['page']) ? $this->postData['page'] : 0;
		// $keywords = isset($this->postData['keywords']) ? $this->postData['keywords'] : '';
		// $couponsno = isset($this->postData['couponsno']) ? $this->postData['couponsno'] : '';
		$limit = 10;
		$where['userid'] = $this->uinfo['userid'];
		if ($state == 'nouse') {
			$where['status'] = 0;
		} elseif ($state == 'used') {
			$where['status'] = 1;
		}
		//  else {
		// 	$where['status'] = ['neq', 2];
		// }
		// if ($couponsno) {
		// 	$where['couponsno'] = $couponsno;
		// }elseif ($keywords) {
		// 	$where['couponsno'] = ['like', "%$keywords%"];
		// }
		$list = model('MemberCoupons')->where($where)->order('id desc')->select();
		if ($list === false) {
			return $this->app_error('获取失败，请重新获取');
		}
		foreach ($list as $key => $value) {
			$activatetime = $list[$key]['activatetime'];
			$currtime = time();
			if ($activatetime > $currtime) {
				$list[$key]['activateable'] = 0;
			} else {
				$list[$key]['activateable'] = 1;
			}
			//
			$qrcode = $list[$key]['image'];
			if ($qrcode) {
				continue;
			}
			$qrcode = createqr($list[$key]['couponsno'], 200);
			if ($qrcode) {
				model('MemberCoupons')->where('couponsno', $list[$key]['couponsno'])->update(['image' => APP_URL . $qrcode]);
				$list[$key]['image'] = APP_URL . $qrcode;
			}
		}
		$total = model('MemberCoupons')->where($where)->count();
		return $this->app_success(['list' => $list], '', 200, ['pages' => ceil($total / $limit)]);
	}

	//获取优惠券详情
	public function coupons_detail()
	{
		$id = $this->postData['id'];
		if ($id != null) {
			$couponsinfo = model('MemberCoupons')->where('id', $id)->find();
			if ($couponsinfo != null) {
				if ($couponsinfo['ownerid']) {
					$couponsinfo['isgive'] = 0;
				}
				//
				$activatetime = $couponsinfo['activatetime'];
				$currtime = time();
				if ($activatetime > $currtime) {
					$couponsinfo['activateable'] = 0;
				} else {
					$couponsinfo['activateable'] = 1;
				}
				//
				return $this->app_success($couponsinfo);
			} else {
				return $this->app_error('优惠券不存在，请重试');
			}
		} else {
			return $this->app_error('参数错误');
		}
	}
	// 核销搜索列表
	public function hexiao_list()
	{
		$keywords = isset($this->postData['keywords']) ? $this->postData['keywords'] : '';
		$couponsno = isset($this->postData['couponsno']) ? $this->postData['couponsno'] : '';
		$page = isset($this->postData['page']) ? $this->postData['page'] : 0;
		$limit = 10;
		$where = [];
		if ($couponsno) {
			$where['couponsno'] = $couponsno;
		} elseif ($keywords) {
			// $where['couponsno'] = ['like', "%$keywords%"];
			$where['couponsno'] = $keywords;
		}
		$cinfo = model('MemberCoupons')->where($where)->find();
		$cid = isset($cinfo['couponsid']) ? $cinfo['couponsid'] : 0;
		if ($cid == 11 && isset($cinfo['ownerid']) && $cinfo['ownerid']) { // 如果是2000购车券
			$dealerid = model('AdminUser')->where('userid', $cinfo['ownerid'])->value('dealerid');
			$mydealerid = $this->uinfo['dealerid'];
			if ($dealerid != $mydealerid) {
				return $this->app_error('获取失败，请重新获取');
			}
		}
		if (empty($where)) {
			return $this->app_success();
		}
		if ($this->uinfo['roleid'] == 8) {
			$where['type'] = ['in', ['gouche', 'tongyong']];
		} elseif ($this->uinfo['roleid'] == 10) {
			$where['type'] = ['in', ['xubao', 'tongyong']];
		} elseif ($this->uinfo['roleid'] == 11) {
			$where['type'] = ['in', ['baoyang', 'tongyong']];
		}
		$where['status'] = 0;
		if ($cid != 11) {
			$where['dealerid'] = $this->uinfo['dealerid'];
		}
		if ($cid == 11) {
			$where['isactivate'] = 1;
		}
		$list = model('MemberCoupons')->where($where)->order('id desc')->page($page, $limit)->select();
		if ($list === false) {
			return $this->app_error('获取失败，请重新获取');
		}
		$total = model('MemberCoupons')->where($where)->count();
		return $this->app_success(['list' => $list], '', 200, ['pages' => ceil($total / $limit)]);
	}
	// 增值总监  售后总监核销
	public function hexiao()
	{
		if (!isset($this->postData['mcid']) || !$this->postData['mcid']) {
			return $this->app_error('请选择要使用的优惠券');
		}
		$mcid = $this->postData['mcid'];
		$info = model('MemberCoupons')->where('id', $mcid)->find();
		$canceltype = model('Coupons')->where('id', $info['couponsid'])->value('canceltype');
		if ($canceltype == 'shouhou' && $this->uinfo['roleid'] != 11) {
			return $this->app_error('权限不足，无法操作');
		} elseif ($canceltype == 'zengzhi' && $this->uinfo['roleid'] != 10) {
			return $this->app_error('权限不足，无法操作');
		} elseif (($canceltype == 'xiaoshou') && $this->uinfo['roleid'] != 8) {
			return $this->app_error('权限不足，无法操作');
		}
		if ($info['status'] == 1) {
			return $this->app_error('该权益已经使用，请选择使用其他权益');
		} elseif ($info['status'] == 2 || $info['endtime'] < time()) {
			return $this->app_error('该权益已经失效，请选择使用其他权益');
		}
		$update = ['status' => 1, 'usetime' => time(), 'adminid' => $this->uinfo['userid']];
		if ($info['couponsid'] == 4) {
			// if (!isset($this->postData['licenseno']) || !$this->postData['licenseno']) {
			// 	return $this->app_error('请输入免费保养车辆的车牌号');
			// }
			// $lastyear = strtotime("-1 year");
			$count = model('MemberCoupons')->where('licenseno', $info['licenseno'])->where('status', '1')->count();
			trace($count);
			if ($count) {
				return $this->app_error('一辆车只能使用一张免费保养券，对您带来的不便敬请谅解！');
			}
			// $update['licenseno'] = $info['licenseno'];
		}
		$ids = model('MemberCoupons')->where('id', $mcid)->update($update);
		if ($ids === false) {
			return $this->app_error('优惠券使用失败，请充值提交');
		}
		return $this->app_success();
	}

	// 激活免费保养券
	public function activate_coupons()
	{
		$isactivate = model('MemberCoupons')->activate($this->postData);
		if (!$isactivate) {
			return $this->app_error($this->modelDb->getError());
		}
		return $this->app_success([], '激活成功');
	}

	// 赠送优惠券
	public function set_coupons()
	{
		$cid = $this->postData['cid'];
		$ownerid = $this->postData['ownerid'];
		if ($ownerid == $this->uinfo['userid']) { // 如果是自己分享给自己的话，是不能领取的
			return $this->app_success();
		}
		$vip = model('AdminUser')->where('userid', $this->uinfo['userid'])->value('vip');
		if ($vip) { // 荣耀会员无法领取
			return $this->app_success();
		}
		$count = model('MemberCoupons')->where('userid', $this->uinfo['userid'])->where('couponsid', $cid)->where('ownerid', $ownerid)->count();
		if (!$count) { // 如果用户没有该券
			exec_php('/index/cron/send_coupons', ['type' => 'give', 'couponsid' => $cid, 'userids' => $this->uinfo['userid'], 'ownerid' => $ownerid]);
		}
		return $this->app_success();
	}

	// 获取平台赠送给荣耀会员的券
	public function vip_coupons()
	{
		$where['createrid'] = 1;
		$where['type'] = ['neq', 'tongyong'];
		$list = $this->modelDb->where($where)->column('name');
		return $this->app_success($list);
	}

	// 获取2000购车券的未激活数量（最多可激活1000张）
	public function activatenum()
	{
		$count = model('MemberCoupons')->where('couponsid', 11)->where('isactivate', 1)->count();
		//
		$data['notactivatenum'] = 1000 - $count;
		return $this->app_success($data);
	}

	// public function appendcoupons()
	// {
	// 	// $filternumber = model('FilterNumber')->column('mobile');
	// 	// $list = model('AdminUser')->where('vip', 1)->where('addtime', '<=', 1582992000)->where('phone', 'not in', $filternumber)->limit(100)->column('userid');
	// 	exec_php('/index/cron/send_coupons', ['type' => 'append_coupons_vip', 'couponsid' => 11]);
	// 	return $this->app_success();
	// }
}
